package napier.edu.chrisMann.retrollerClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.*;
import android.widget.Button;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;


public class RetrollerClient extends Activity {

    private final static String TAG = "Retroller"; 
    Socket ClientSocket = null;
	DataOutputStream out = null;
	private Button leftButton;
	private Button rightButton;
	private Button upButton;
	private Button downButton;
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button bShould1;
	private Button bShould2;
	private Button bStart;
	private Button bSelect;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        Connect();
        setContentView(R.layout.main);
        
        //LEFT BUTTON//
        this.leftButton = (Button)this.findViewById(R.id.left);
        this.leftButton.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("LT:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("LT:UP");
            		break;
            	}
				return true;
			}
          });

        //RIGHT BUTTON//
        this.rightButton = (Button)this.findViewById(R.id.right);
        this.rightButton.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("RT:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("RT:UP");
            		break;
            	}
				return true;
			}
          });

        //UP BUTTON//
        this.upButton = (Button)this.findViewById(R.id.up);
        this.upButton.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("UP:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("UP:UP");
            		break;
            	}
				return true;
			}
          });

        //DOWN BUTTON//
        this.downButton = (Button)this.findViewById(R.id.down);
        this.downButton.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("DN:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("DN:UP");
            		break;
            	}
				return true;
			}
          });

        //BUTTON1//
        this.button1 = (Button)this.findViewById(R.id.button_1);
        this.button1.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("B0:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("B0:UP");
            		break;
            	}
				return true;
			}
          });

        //BUTTON2//
        this.button2 = (Button)this.findViewById(R.id.button_2);
        this.button2.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("B1:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("B1:UP");
            		break;
            	}
				return true;
			}
          });

        //BUTTON3//
        this.button3 = (Button)this.findViewById(R.id.button_3);
        this.button3.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("B2:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("B2:UP");
            		break;
            	}
				return true;
			}
          });

        //BUTTON4//
        this.button4 = (Button)this.findViewById(R.id.button_4);
        this.button4.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("B3:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("B3:UP");
            		break;
            	}
				return true;
			}
          });

      //LEFT SHOULDER//
        this.bShould1 = (Button)this.findViewById(R.id.lShould);
        this.bShould1.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("S1:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("S1:UP");
            		break;
            	}
				return true;
			}
          });
        
      //RIGHT SHOULDER//
        this.bShould2 = (Button)this.findViewById(R.id.rShould);
        this.bShould2.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("S2:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("S2:UP");
            		break;
            	}
				return true;
			}
          });
        
      //START//
        this.bStart = (Button)this.findViewById(R.id.start);
        this.bStart.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("ST:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("ST:UP");
            		break;
            	}
				return true;
			}
          });
        
        //START//
        this.bSelect = (Button)this.findViewById(R.id.select);
        this.bSelect.setOnTouchListener(new OnTouchListener() {
            @Override
			public boolean onTouch(View view, MotionEvent event) {
            	switch(event.getAction()){
            	case MotionEvent.ACTION_DOWN: 
            		SendCommand("SE:DN");
            		break;
            	case MotionEvent.ACTION_UP: 
            		SendCommand("SE:UP");
            		break;
            	}
				return true;
			}
          });
    }
    
    public void OnPause(){
    	super.onPause();
    	Disconnect();
    	this.finish();
    }
    
    public void onFreeze(){
    	
    }
    
    public void onDestroy()
    {
    	Disconnect();
    	super.onDestroy();
    }
    
    public void Connect(){
    	try{
    		Log.v(TAG, "Attempting to connect!");
    		ClientSocket = new Socket("192.168.1.2", 2004);
    		ClientSocket.setSoTimeout(5);
    	} catch (IOException IOE) {
    		Log.e(TAG, IOE.getMessage());
    	}
    	try{
    		out = new DataOutputStream(ClientSocket.getOutputStream());
        } catch (UnknownHostException UHE) {
        	Log.e(TAG, UHE.getMessage());
        } catch (IOException IOE) {
        	Log.e(TAG, IOE.getMessage());
		}
        SendCommand("CONN");
    }
    
    public void Disconnect(){
    	SendCommand("DISC");
    }
    
    public void SendCommand(String command){
    	try {
			out.writeUTF(command);
			out.flush();
			Log.v(TAG,"Sending command: "+command);
		} catch (IOException IOE) {
			Log.e(TAG, IOE.getMessage());
		}
    }
}